const UserService = require('../../services/admin/UserService');
const JWT = require('../../utils/JWT');
const UserController = {
  // 登陆
  login: async (req, res) => {
    try {
      const result = await UserService.login(req.body);
      if (result.length !== 0) {
        const { _id, username, role, introduction, avatar, gender } = result[0];
        const token = JWT.generateToken({ _id, username });
        res.header('Authorization', token);
        res.setHeader('Access-Control-Expose-Headers', 'Authorization'); //  手动添加返回头部
        res.send({
          success: true,
          code: 1,
          msg: '登陆成功',
          data: {
            username: username,
            role: role,
            introduction: introduction,
            avatar: avatar,
            gender: gender || 0, // 0 保密 1 男 2 女
          },
        });
      } else {
        res.send({ success: false, code: 0, msg: '登陆失败' });
      }
    } catch (error) {
      res.send({ success: false, code: 0, msg: '登陆失败' + error });
    }
  },
  // 注册
  register: async (req, res) => {
    try {
      const { username } = req.body;
      const user = await UserService.findOneUser({ username });
      console.log(user, '=====');
      if (user) {
        res.send({ success: false, code: 0, msg: '用户名已存在' });
        return;
      }

      const result = await UserService.register(req.body);
      if (result) {
        res.send({
          success: true,
          code: 1,
          msg: '注册成功',
        });
      } else {
        res.send({ success: false, code: 0, msg: '注册失败' });
      }
    } catch (error) {
      res.send({ success: false, code: 0, msg: '注册失败' + error });
    }
  },
  // 编辑
  edit: async (req, res) => {
    try {
      let { _id, ...data } = req.body;
      // 若 id 不存在。则更新自身，若id 存在， 则更新id 用户
      if (!_id) {
        const { authorization = '' } = req.headers;
        const token = authorization.split(' ')[1];
        _id = JWT.verifyToken(token)._id;
      }

      const result = await UserService.edit(_id, data);
      if (result) {
        res.send({
          success: true,
          code: 1,
          msg: '更新成功',
          data: data,
        });
      } else {
        res.send({ success: false, code: 0, msg: '更新成功' });
      }
    } catch (error) {
      res.send({ success: false, code: 0, msg: '更新失败' + error });
    }
  },

  list: async (req, res) => {
    try {
      // 查找所有
      const list = await UserService.list(req.body);
      if (list) {
        res.send({
          data: list,
          msg: '查询成功',
          code: 1,
          success: true,
        });
      } else {
        res.send({
          msg: '查询失败',
          code: 0,
          success: false,
        });
      }
    } catch (error) {
      res.send({
        msg: '查询失败' + error,
        code: 0,
        success: false,
      });
    }
  },

  delete: async (req, res) => {
    try {
      // 删除
      const { _id } = req.body;
      const result = await UserService.delete(_id);
      if (result) {
        res.send({
          success: true,
          code: 1,
          msg: '删除成功',
        });
      } else {
        res.send({ success: false, code: 0, msg: '删除失败' });
      }
    } catch (error) {
      res.send({ success: false, code: 0, msg: '删除失败' + error });
    }
  },
  // 查询个人信息
  findById: async (req, res) => {
    try {
      // 查询单个
      const { _id } = req.body;
      const result = await UserService.findOneUser({ _id });
      if (result) {
        res.send({
          success: true,
          code: 1,
          msg: '查询成功',
          data: result,
        });
      } else {
        res.send({ success: false, code: 0, msg: '查询失败' });
      }
    } catch (error) {
      res.send({ success: false, code: 0, msg: '查询失败' + error });
    }
  },
};

module.exports = UserController;
